<?php

namespace app\api\model\order;

use think\Model;


class OrderGoods extends Model
{

    

    

    // 表名
    protected $name = 'order_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'time_text'
    ];
    

    



    public function getTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function goodsCategory()
    {
        return $this->hasOne('\app\api\model\goods\GoodsCategory', 'id', 'goods_category_id')->field('id,title as goods_category_name')->bind(['goods_category_name']);
    }

    public function biaobenRongqi()
    {
        return $this->hasOne('\app\api\model\goods\BiaobenRongqi', 'id', 'biaoben_rongqi_id')->field('id,title as biaoben_rongqi_name')->bind(['biaoben_rongqi_name']);
    }

    public function biaobenType()
    {
        return $this->hasOne('\app\api\model\goods\BiaobenType', 'id', 'biaoben_type_id')->field('id,title as biaoben_type_name')->bind(['biaoben_type_name']);
    }


    public function orderTube()
    {
        return $this->hasMany('\app\api\model\order\OrderTube', 'order_goods_id', 'order_goods_id')->with(['biaobenRongqi']);
    }

}
